<template>
  <div class="navBarContainer">
    <div class="navBar">
      <div class="barItem" :class="index == activeNum ? 'active' : ''" v-for="(item, index) in navBarItem" :key="index" @click="clickBarItem(item.path, index)">
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, onMounted, toRefs } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'NavBar',
  props: {
    navBarItem: {
      type: Array,
      default() {
        return []
      }
    }
  },
  setup(props, ctx) {
    const data = reactive({
      activeNum: 0
    })
    const route = useRoute()
    const path = decodeURI(route.path)
    onMounted(() => {
      props.navBarItem.forEach((v, i) => {
        if (path.indexOf(v.path) !== -1) {
          data.activeNum = i
        }
      })
    })
    const clickBarItem = (path, i) => {
      data.activeNum = i
      ctx.emit('clickBarItem', path)
    }
    return {
      clickBarItem,
      ...toRefs(data)
    }
  }
}
</script>

<style scoped>
.navBarContainer {
  height: 60px;
  width: 100%;
  position: relative;
}

.navBar {
  display: flex;
  position: relative;
  left: 0px;
  top: 0;
  background-color: #fff;
  z-index: 100;
  width: 100%;
  color: rgb(49, 49, 49);
  align-items: center;
  padding: 10px 0 20px 20px;
}

.barItem {
  font-size: 13px;
  margin: 0 10px;
  text-align: center;
  cursor: pointer;
}

.active {
  font-size: 16px;
  color: black;
  font-weight: 600;
  padding-bottom: 5px;
  border-bottom: 3px solid #ec4141;
  transform: translateY(4px);
}
</style>